var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timeList:[{startTime:'',endTime:'',sTimeNum:'',eTimeNum:'',id:0,type:['start','end']}],
    submit:0,
    isShowTime:false,
    isShowTime2:false,
    timeIndex:0,
    startTime:'',
    endTime:'',
    signImg:''
  },
  beginSign(e){  
    var that=this
    if(!Boolean(that.data.signImg)){
      Store.dispatch({
        type: 'SET_FROM',
        form: {
          key: 'workSign', 
        },
      })
      $.linkurl(`/pages/components/signatrue/signAture?ticketNumber=${that.data.ticketNumber}`) 
    }
    // wx.showModal({
    //   title: '提示',
    //   content: '使用原有签名或者手动签名',
    //   confirmText:'原有签名',
    //   cancelText:'手动签名',
    //   success (res) {
    //     if (res.confirm) {
          
    //       that.setData({
    //         signImg: wx.getStorageSync('signUrl'),
    //       })
    //     } else if (res.cancel) {
    //       Store.dispatch({
    //         type: 'SET_FROM',
    //         form: {
    //           key: 'workSign', 
    //         },
    //       })
    //       $.linkurl(`/pages/components/signatrue/signAture?ticketNumber=${that.data.ticketNumber}`) 
    //     }
    //   }
    // })
  },
  bigSign(e){
    const {img}=$.detail(e)
    wx.previewImage({
      current: '', // 当前显示图片的链接，不填则默认为urls的第一张
      urls: [img] // 需要预览的图片链接列表
    })
  },
  addTime(){
    var timeArr=this.data.timeList
    timeArr.push({startTime:'',endTime:'',id:timeArr.length,sTimeNum:'',eTimeNum:''})
    this.setData({timeList:timeArr})
    console.log(this.data.timeList)
  },
   //##input 输入##
  input_handle(e) {
    this.setData({
      [e.currentTarget.dataset.key]: e.detail.value
    })
  },
  cancelBack(){
    $.linkurl('','back')
  },
  setReject(){
    if($.isNull(this.data.rejectReason)){
      $.toast("驳回原因不能为空")
        return
    }
    if(!Boolean(this.data.signImg)){
      return $.toast('当前无默认签名，请手动填写！')
    }
    if(this.data.type=='值班人确认'){
      api.$get(api.workTicket.workDelayDutyConfirm, {
        ticketNumber:this.data.ticketNumber,
        approvalResult:false,
        signature:this.data.signImg,
        approvalRemark:this.data.rejectReason
      }).then((res) => {
        $.linkurl('','back')
      })
    }
    if(this.data.type=='签发人确认'){
      api.$get(api.workTicket.workDelayPermitIssuerConfirm, {
        ticketNumber:this.data.ticketNumber,
        approvalResult:false,
        signature:this.data.signImg,
        approvalRemark:this.data.rejectReason
      }).then((res) => {
        $.linkurl('','back')
      })
    }
  },
  openTimeHandle(e) { // 打开时间弹框
    const {item,key}=$.detail(e)
    this.setData({
      timeType:key,
      timeIndex:item.id
    })
    if(this.data.submit=='1'){
      $.toast("已提交无法修改")
  }else{
    if(key=='start'){
      this.setData({
        isShowTime: true
      })
    }else if(key=='end'&&item.startTime!=''){
      this.setData({
        isShowTime2: true
      })
    }else{
      $.toast("请先选择开始时间")
    }
      
    }
  },
  timeConfirmHandle(e) { // 时间组件传出来的数据
    var { time, key,timeNum } = e.detail
    var timeArr=this.data.timeList
    timeArr[this.data.timeIndex][key]=time
    
    if(key=='startTime'){
      timeArr[this.data.timeIndex]['sTimeNum']=timeNum
    }else{
      timeArr[this.data.timeIndex]['eTimeNum']=timeNum
    }
    this.setData({
      timeList: timeArr,
      [key]:timeNum
    })
  
    console.log(time, key)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let sdate = new Date('2024-01-09T09:00:00');
    let edate = new Date('2024-09-09T09:00:00');
     // 注意：我添加了 T 来分隔日期和时间  
    // 获取时间戳  
    this.setData({
      startTime:sdate.getTime(),
      endTime:edate.getTime()
    })
    if ('ticketNumber' in options){ 
      this.setData({
        ticketNumber:options.ticketNumber, 
        type:options.type,
      })
    }
  },
  onShow() {
    var form = Store.getState().formInfor.form
    var key = form.key
    console.log(form)
    switch (key) {
      case 'workSign': // 获取签发人签名  
        this.setData({
          signImg: form.selected,
        })
        break 
    
      default:
        this.setData({
          signImg: wx.getStorageSync('signUrl'),
        })
        break;
    }
    console.log(this.data.signImg)
  },

  openLoseHandle(){
    $.linkurl('','back')
  },
  subExtension(){
    if(!Boolean(this.data.signImg)){
      return $.toast('当前无默认签名，请手动填写！')
    }
    if(this.data.type=='负责人提交'){
      if(this.data.timeList[0].startTime==''||this.data.timeList[0].startTime==''){
        $.toast('请选择开工、收工时间')
        return false
      }
      
      api.$get(api.workTicket.submitWorkDailyLog, {
        ticketNumber:this.data.ticketNumber,
        startTime:this.data.timeList[0].startTime,
        endTime:this.data.timeList[0].endTime,
        signature:this.data.signImg,
      }).then((res) => {
        $.linkurl('','back')
      })
    }
    if(this.data.type=='许可人审核'){
      api.$get(api.workTicket.dailyLogPermitIssuerConfirm, {
        ticketNumber:this.data.ticketNumber,
        logId:true,
        signature:this.data.signImg,
      }).then((res) => {
        $.linkurl('','back')
      })
    }
  }
})